﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ThumbcacheHelper
{
    public partial class FrmInput : Form
    {
        Dictionary<FileId, String> _inputFiles = new Dictionary<FileId, string>();

        public FrmInput()
        {
            InitializeComponent();            
        }

        public Dictionary<FileId, String> InputFiles { get { return _inputFiles; } }

        private void btnOk_Click(object sender, EventArgs e)
        {
            int missing = 0;
            foreach (Control c in this.Controls)
            {
                if (c is FileTextBox)
                {
                    FileTextBox ftxt = (FileTextBox)c;
                    if (!String.IsNullOrWhiteSpace(ftxt.Text))
                    {
                        FileInfo fi = new FileInfo(ftxt.Text);
                        if (fi.Exists)
                        {
                            _inputFiles[ftxt.FileID] = c.Text;
                        }
                        else
                        {
                            ftxt.ForeColor = Color.IndianRed;
                            missing++;
                        }
                    }
                }
            }

            if (missing > 0)
            {
                MessageBox.Show(String.Format("{0} selected {1} could not be found.", missing, missing == 1 ? "file" : "files"), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private string getFile()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "DB/EDB Files (*.db;*.edb)|*.db;*.edb|All Files|*";

            if (ofd.ShowDialog() == DialogResult.OK)
                return ofd.FileName;
            else
                return null;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            foreach (Control c in this.Controls)
            {
                if (c is TextBox)
                {
                    c.Text = String.Empty;
                }
            }
        }

        private void frmInput_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.All;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void frmInput_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                foreach(string file in files)
                {
                    FileInfo fi = new FileInfo(file);
                    if ((fi.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
                    {
                        processFolder(file);
                        break;
                    }
                    else
                    {
                        FileId key = FileIdFromFileName(fi.Name);
                        if (key != FileId.Unknown)
                        {
                            try
                            {
                                _inputFiles.Add(key, fi.FullName);
                            }
                            catch (ArgumentException)
                            {
                                _inputFiles[key] = fi.FullName;
                            }
                        }
                    }
                }

                populateTextboxes();
            }
        }

        private FileId FileIdFromFileName(string fileName)
        {
            if (fileName.Contains("thumbcache_32"))
                return FileId.Thumbs32;
            else if (fileName.Contains("thumbcache_96"))
                return FileId.Thumbs96;
            else if (fileName.Contains("thumbcache_256"))
                return FileId.Thumbs256;
            else if (fileName.Contains("thumbcache_1024"))
                return FileId.Thumbs1024;
            else if (fileName.Contains("thumbcache_idx"))
                return FileId.ThumbsIdx;
            else if (fileName.Contains("Windows.edb"))
                return FileId.WinEdb;
            else
                return FileId.Unknown;
        }

        private void populateTextboxes()
        {
            foreach (Control c in Controls)
            {
                if (c is FileTextBox)
                {
                    FileTextBox ftxt = (FileTextBox)c;
                    if (_inputFiles.ContainsKey(ftxt.FileID))
                    {
                        ftxt.Text = String.IsNullOrWhiteSpace(_inputFiles[ftxt.FileID]) ? String.Empty : _inputFiles[ftxt.FileID];
                    }
                }
            }            
        }

        private void processFolder(string Folder)
        {
            FileInfo[] subFiles = new DirectoryInfo(Folder).GetFiles();
            foreach (FileInfo subfi in subFiles)
            {
                FileId key = FileIdFromFileName(subfi.Name);
                if (key != FileId.Unknown)
                {
                    try
                    {
                        _inputFiles.Add(key, subfi.FullName);
                    }
                    catch (ArgumentException)
                    {
                        _inputFiles[key] = subfi.FullName;
                    }
                }
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            string file = getFile();
            if (file != null)
            {
                FileId fid = ((FileBrowserButton)sender).FileID;
                foreach (Control c in Controls)
                {
                    if (c is FileTextBox && ((FileTextBox)c).FileID == fid)
                    {
                        c.Text = file;
                    }
                }
            }

        }

        private void btnFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.ShowNewFolderButton = false;
            fbd.RootFolder = Environment.SpecialFolder.Desktop;
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                processFolder(fbd.SelectedPath);
                populateTextboxes();
            }
        }        
    }
}
